<template>
    <div class="dashboard-container">
        <global-search @submitSearch="submitSearch" class="global-tool-search"/>
        <div class="page-height-box">
            <view-card :cardData="cardData"/>
            <div class="echart-table-box">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <div class="content">
                            <div class="title-tool-btn">
                                <p class="title">产能分析</p>
                                <el-button type="text" class="export-btn" @click="toPageList('Service')">
                                    明细
                                    <svg-icon icon-class="right-circled"/>
                                </el-button>
                            </div>
                            <data-chart :chartData="capacityEchart"/>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="content" style="padding-right: 15px">
                            <div class="title-tool-btn">
                                <p class="title">效率分析</p>
                                <el-button type="text" class="export-btn" @click="toPageList('Efficiency')">
                                    明细
                                    <svg-icon icon-class="right-circled"/>
                                </el-button>
                            </div>
                            <scatter-plot :chartData="efficiencyEchart"/>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="content">
                            <div class="title-tool-btn">
                                <p class="title">安全分析</p>
                                <el-button type="text" class="export-btn" @click="toPageList('Safety')">
                                    明细
                                    <svg-icon icon-class="right-circled"/>
                                </el-button>
                            </div>
                            <data-chart :chartData="safetyEchart"/>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="content">
                            <div class="title-tool-btn">
                                <p class="title">费用分析</p>
                                <el-button type="text" class="export-btn" @click="toPageList('Expenses')">
                                    明细
                                    <svg-icon icon-class="right-circled"/>
                                </el-button>
                            </div>
                            <div class="pie-tips">
                                <div>
                                    <p>{{total | getMoneyNum}}元</p>
                                    <p>住院总费用</p>
                                </div>
                                <div>
                                    <p>{{average | getMoneyNum}}元</p>
                                    <p>平均住院费用</p>
                                </div>
                                <div>
                                    <p>{{drgIncome | getMoneyNum}}元</p>
                                    <p>DRG盈亏</p>
                                </div>
                            </div>
                            <data-chart :chartData="expenseEchart"/>
                        </div>
                    </el-col>
                </el-row>
            </div>
        </div>
    </div>
</template>

<script>
    import {
        overviewGroup,
        overviewWeight,
        overviewCoverGroup,
        overviewCapacity,
        overviewEfficiency,
        overviewSecurity,
        overviewFee
    } from '@/api/overview'
    import ScatterPlot from '@/components/ScatterPlot'
    import DataChart from '@/components/DataChart'
    import ViewCard from '@/components/ViewCard'
    import {getMoneyNum} from '@/utils/filters'

    export default {
        name: 'Dashboard',
        data () {
            return {
                // 卡片数据
                cardData: [
                    {
                        title: '总病案数',
                        data: 0,
                        tips: '符合当前筛选条件下的病案数',
                        pageName: 'OverviewListIndex',
                        tab: 'all'
                    }, {
                        title: '入组病案数',
                        data: 0,
                        scale: '0%',
                        tips: '已入组的病案数',
                        pageName: 'OverviewListIndex',
                        tab: 'included'
                    }, {
                        title: '未入组病案数',
                        data: 0,
                        scale: '0%',
                        tips: '无法入组的数据,(排除组不计算到未入组中)',
                        pageName: 'OverviewListIndex',
                        tab: 'not'
                    }, {
                        title: '排除组病案数',
                        data: 0,
                        scale: '0%',
                        tips: '根据《排除组规则》排除在外的病案数',
                        pageName: 'OverviewListIndex',
                        tab: 'exclude'
                    }, {
                        title: '总权重(RW)',
                        data: 0,
                        tips: '某DRG费用权重×该医院DRG病案数',
                        pageName: 'OverviewListIndex',
                        tab: 'included'
                    }, {
                        title: 'CMI指数',
                        data: 0,
                        tips: '（某DRG费用权重×该医院DRG病案数）/该医院全体病案数',
                        pageName: 'OverviewListIndex',
                        tab: 'included'
                    }, {
                        title: '31天再入院病案数',
                        data: 0,
                        scale: '0%',
                        tips: '31天内有相同病案号住院的人次, 过滤条件:过滤病案首页“是否有出院31天内在住院计划字段选择有的病案”',
                        pageName: 'OverviewAgainIndex'
                    }, {
                        title: '死亡例数',
                        data: 0,
                        scale: '0%',
                        tips: '符合当前筛选条件下死亡的病案数',
                        pageName: 'OverviewDeathsIndex'
                    }, {
                        title: '平均住院日',
                        data: 0,
                        tips: '出院患者占用总床日数/同期出院人数',
                        pageName: 'OverviewListIndex',
                        tab: 'included'
                    }, {
                        title: '覆盖MDC组数',
                        data: 0,
                        tips: '筛选条件下的MDC数(去重)',
                        pageName: 'OverviewCover',
                        tab: 'mdc'
                    }, {
                        title: '覆盖ADRG组数',
                        data: 0,
                        tips: '筛选条件下的ADRG数(去重)',
                        pageName: 'OverviewCover',
                        tab: 'adrg'
                    }, {
                        title: '覆盖DRGs组数',
                        data: 0,
                        tips: '筛选条件下的DRGs数(去重)',
                        pageName: 'OverviewCover',
                        tab: 'drgs'
                    }],
                // 产能分析
                capacityEchart: {
                    xAxis: [],
                    series: [{
                        name: 'DRGs入组数',
                        data: []
                    }, {
                        name: 'DRGs总权重',
                        data: []
                    }, {
                        name: 'CMI指数',
                        data: []
                    }],
                    rightData: ['DRGs入组数', 'DRGs总权重'],
                    id: 'capacityEchart'
                },
                // 效率分析
                efficiencyEchart: [],
                // 安全分析
                safetyEchart: {
                    xAxis: [],
                    series: [{
                        name: '低风险组死亡',
                        data: []
                    }, {
                        name: '中低风险组死亡',
                        data: []
                    }, {
                        name: '中高风险组死亡',
                        data: []
                    }, {
                        name: '高风险组死亡',
                        data: []
                    }],
                    rate: [],
                    yAxisName: ['(人)'],
                    id: 'safetyEchart'
                },
                // 费用分析
                expenseEchart: {
                    type: 'pie',
                    series: [],
                    id: 'expenseEchart',
                    height: '276px'
                },
                // 住院总费用
                total: 0,
                // 平均住院费用
                average: 0,
                // DRG盈亏
                drgIncome: 0,
                // 使用储存的数据，不去请求
                useStoreGroup: false,
                useStoreWeight: false,
                useStoreCoverGroup: false,
                useStoreCapacity: false,
                useStoreEfficiency: false,
                useStoreSecurity: false,
                useStoreFee: false
            }
        },
        created () {
            // tags判断入口是否从选项卡进入
            this.useStoreGroup = this.$route.query.tags === 'tags'
            this.useStoreWeight = this.$route.query.tags === 'tags'
            this.useStoreCoverGroup = this.$route.query.tags === 'tags'
            this.useStoreCapacity = this.$route.query.tags === 'tags'
            this.useStoreEfficiency = this.$route.query.tags === 'tags'
            this.useStoreSecurity = this.$route.query.tags === 'tags'
            this.useStoreFee = this.$route.query.tags === 'tags'
        },
        mounted () {
        },
        methods: {
            /**
             * 提交查询
             * @param form 筛选条件
             */
            submitSearch (form) {
                this.getOverviewGroup(form)
                this.getOverviewWeight(form)
                this.getOverviewCoverGroup(form)
                this.getOverviewCapacity(form)
                this.getOverviewEfficiency(form)
                this.getOverviewSecurity(form)
                this.getOverviewFee(form)
            },
            /**
             * 病案数，死亡数，入组，未入组，排除组查询
             * @param params 查询参数
             */
            getOverviewGroup (params) {
                if (this.getSession(this.$route.name, 'overviewGroup') && this.useStoreGroup) {
                    this.useStoreGroup = false
                    let res = this.getSession(this.$route.name, 'overviewGroup')
                    if (res.code.toString() === '200') {
                        this.cardData[0].data = res.data.caseNum
                        this.cardData[1].data = res.data.groupNum
                        this.cardData[1].scale = res.data.groupRate
                        this.cardData[2].data = res.data.ungroupNum
                        this.cardData[2].scale = res.data.ungroupRate
                        this.cardData[3].data = res.data.excludedGroupNum
                        this.cardData[3].scale = res.data.excludedGroupRate
                        this.cardData[6].data = res.data.rehosNum
                        this.cardData[6].scale = res.data.rehosRate
                        this.cardData[7].data = res.data.deathNum
                        this.cardData[7].scale = res.data.deathRate
                    }
                } else {
                    overviewGroup(params).then(res => {
                        this.useStoreGroup = false
                        this.setSession(this.$route.name, {overviewGroup: res})
                        if (res.code.toString() === '200') {
                            this.cardData[0].data = res.data.caseNum
                            this.cardData[1].data = res.data.groupNum
                            this.cardData[1].scale = res.data.groupRate
                            this.cardData[2].data = res.data.ungroupNum
                            this.cardData[2].scale = res.data.ungroupRate
                            this.cardData[3].data = res.data.excludedGroupNum
                            this.cardData[3].scale = res.data.excludedGroupRate
                            this.cardData[6].data = res.data.rehosNum
                            this.cardData[6].scale = res.data.rehosRate
                            this.cardData[7].data = res.data.deathNum
                            this.cardData[7].scale = res.data.deathRate
                        }
                    })
                }
            },
            /**
             * 权重和CMI系数查询
             * @param params 查询参数
             */
            getOverviewWeight (params) {
                if (this.getSession(this.$route.name, 'overviewWeight') && this.useStoreWeight) {
                    this.useStoreWeight = false
                    let res = this.getSession(this.$route.name, 'overviewWeight')
                    if (res.code.toString() === '200') {
                        this.cardData[4].data = res.data.weight
                        this.cardData[5].data = res.data.cmiIndex
                    }
                } else {
                    overviewWeight(params).then(res => {
                        this.useStoreWeight = false
                        this.setSession(this.$route.name, {overviewWeight: res})
                        if (res.code.toString() === '200') {
                            this.cardData[4].data = res.data.weight
                            this.cardData[5].data = res.data.cmiIndex
                        }
                    })
                }
            },
            /**
             * 覆盖组和平均住院日查询
             * @param params 查询参数
             */
            getOverviewCoverGroup (params) {
                if (this.getSession(this.$route.name, 'overviewCoverGroup') && this.useStoreCoverGroup) {
                    this.useStoreCoverGroup = false
                    let res = this.getSession(this.$route.name, 'overviewCoverGroup')
                    if (res.code.toString() === '200') {
                        this.cardData[8].data = res.data.averageHosDay
                        this.cardData[9].data = res.data.mdc
                        this.cardData[10].data = res.data.adrg
                        this.cardData[11].data = res.data.drGs
                    }
                } else {
                    overviewCoverGroup(params).then(res => {
                        this.useStoreCoverGroup = false
                        this.setSession(this.$route.name, {overviewCoverGroup: res})
                        if (res.code.toString() === '200') {
                            this.cardData[8].data = res.data.averageHosDay
                            this.cardData[9].data = res.data.mdc
                            this.cardData[10].data = res.data.adrg
                            this.cardData[11].data = res.data.drGs
                        }
                    })
                }
            },
            /**
             * 产能分析查询
             * @param params 查询参数
             */
            getOverviewCapacity (params) {
                if (this.getSession(this.$route.name, 'overviewCapacity') && this.useStoreCapacity) {
                    this.useStoreCapacity = false
                    let res = this.getSession(this.$route.name, 'overviewCapacity')
                    if (res.code.toString() === '200') {
                        this.capacityEchart = {
                            xAxis: res.data.month,
                            series: [{
                                name: 'DRGs入组数',
                                data: res.data.drg
                            }, {
                                name: 'DRGs总权重',
                                data: res.data.weight
                            }, {
                                name: 'CMI指数',
                                data: res.data.cmiIndex
                            }],
                            rightData: ['DRGs入组数', 'DRGs总权重'],
                            id: 'capacityEchart'
                        }
                    }
                } else {
                    overviewCapacity(params).then(res => {
                        this.useStoreCapacity = false
                        this.setSession(this.$route.name, {overviewCapacity: res})
                        if (res.code.toString() === '200') {
                            this.capacityEchart = {
                                xAxis: res.data.month,
                                series: [{
                                    name: 'DRGs入组数',
                                    data: res.data.drg
                                }, {
                                    name: 'DRGs总权重',
                                    data: res.data.weight
                                }, {
                                    name: 'CMI指数',
                                    data: res.data.cmiIndex
                                }],
                                rightData: ['DRGs入组数', 'DRGs总权重'],
                                id: 'capacityEchart'
                            }
                        }
                    })
                }
            },
            /**
             * 效率分析查询
             * @param params 查询参数
             */
            getOverviewEfficiency (params) {
                this.efficiencyEchart = []
                if (this.getSession(this.$route.name, 'overviewEfficiency') && this.useStoreEfficiency) {
                    this.useStoreEfficiency = false
                    let res = this.getSession(this.$route.name, 'overviewEfficiency')
                    if (res.code.toString() === '200') {
                        res.data.forEach(item => {
                            this.efficiencyEchart.push({
                                name: item.department,
                                xy: [item.timeIndex, item.feeIndex]
                            })
                        })
                    }
                } else {
                    overviewEfficiency(params).then(res => {
                        this.useStoreEfficiency = false
                        this.setSession(this.$route.name, {overviewEfficiency: res})
                        if (res.code.toString() === '200') {
                            res.data.forEach(item => {
                                this.efficiencyEchart.push({
                                    name: item.department,
                                    xy: [item.timeIndex, item.feeIndex]
                                })
                            })
                        }
                    })
                }
            },
            /**
             * 安全性分析查询
             * @param params 查询参数
             */
            getOverviewSecurity (params) {
                if (this.getSession(this.$route.name, 'overviewSecurity') && this.useStoreSecurity) {
                    this.useStoreSecurity = false
                    let res = this.getSession(this.$route.name, 'overviewSecurity')
                    if (res.code.toString() === '200') {
                        this.safetyEchart = {
                            xAxis: res.data.month,
                            series: [{
                                name: '低风险组死亡',
                                data: res.data.lowNum
                            }, {
                                name: '中低风险组死亡',
                                data: res.data.lowMiddleNum
                            }, {
                                name: '中高风险组死亡',
                                data: res.data.middleHighNum
                            }, {
                                name: '高风险组死亡',
                                data: res.data.highNum
                            }],
                            rate: [{
                                name: '低风险组死亡',
                                data: res.data.lowRate
                            }, {
                                name: '中低风险组死亡',
                                data: res.data.lowMiddleRate
                            }, {
                                name: '中高风险组死亡',
                                data: res.data.middleHighRate
                            }, {
                                name: '高风险组死亡',
                                data: res.data.highRate
                            }],
                            yAxisName: ['(人)'],
                            id: 'safetyEchart'
                        }
                    }
                } else {
                    overviewSecurity(params).then(res => {
                        this.useStoreSecurity = false
                        this.setSession(this.$route.name, {overviewSecurity: res})
                        if (res.code.toString() === '200') {
                            this.safetyEchart = {
                                xAxis: res.data.month,
                                series: [{
                                    name: '低风险组死亡',
                                    data: res.data.lowNum
                                }, {
                                    name: '中低风险组死亡',
                                    data: res.data.lowMiddleNum
                                }, {
                                    name: '中高风险组死亡',
                                    data: res.data.middleHighNum
                                }, {
                                    name: '高风险组死亡',
                                    data: res.data.highNum
                                }],
                                rate: [{
                                    name: '低风险组死亡',
                                    data: res.data.lowRate
                                }, {
                                    name: '中低风险组死亡',
                                    data: res.data.lowMiddleRate
                                }, {
                                    name: '中高风险组死亡',
                                    data: res.data.middleHighRate
                                }, {
                                    name: '高风险组死亡',
                                    data: res.data.highRate
                                }],
                                yAxisName: ['(人)'],
                                id: 'safetyEchart'
                            }
                        }
                    })
                }
            },
            /**
             * 费用分析查询
             * @param params 查询参数
             */
            getOverviewFee (params) {
                if (this.getSession(this.$route.name, 'overviewFee') && this.useStoreFee) {
                    this.useStoreFee = false
                    let res = this.getSession(this.$route.name, 'overviewFee')
                    if (res.code.toString() === '200') {
                        this.total = res.data.total
                        this.average = res.data.average
                        this.drgIncome = res.data.drgIncome
                        this.expenseEchart = {
                            type: 'pie',
                            series: [
                                {value: res.data.drug, name: '药品占比'},
                                {value: res.data.martial, name: '耗材占比'},
                                {value: res.data.test, name: '检验占比'},
                                {value: res.data.surgery, name: '手术占比'},
                                {value: res.data.inspection, name: '检查占比'},
                                {value: res.data.other, name: '其他'}
                            ],
                            id: 'expenseEchart',
                            height: '276px'
                        }
                    }
                } else {
                    overviewFee(params).then(res => {
                        this.useStoreFee = false
                        this.setSession(this.$route.name, {overviewFee: res})
                        if (res.code.toString() === '200') {
                            this.total = res.data.total
                            this.average = res.data.average
                            this.drgIncome = res.data.drgIncome
                            this.expenseEchart = {
                                type: 'pie',
                                series: [
                                    {value: res.data.drug, name: '药品占比'},
                                    {value: res.data.martial, name: '耗材占比'},
                                    {value: res.data.test, name: '检验占比'},
                                    {value: res.data.surgery, name: '手术占比'},
                                    {value: res.data.inspection, name: '检查占比'},
                                    {value: res.data.other, name: '其他'}
                                ],
                                id: 'expenseEchart',
                                height: '276px'
                            }
                        }
                    })
                }
            },
            /**
             * 明细页面跳转
             * @param name 页面名称
             */
            toPageList (name) {
                let query = JSON.parse(JSON.stringify(this.$store.state.search.searchForm[this.$route.name]))
                let params = {}
                // 设置目标页筛选条件为当前页面条件(全局的搜索框和form表单)
                if (query) {
                    params = {
                        orgId: query.orgId,
                        department: query.department,
                        depName: query.depName,
                        start: query.start,
                        end: query.end,
                        dep: query.department,
                        timeUnit: query.timeUnit,
                        yearMonth: query.yearMonth,
                        quarter: query.quarter
                    }
                }
                this.setGetStore(name, params)
                this.$router.push({name: name})
            }
        },
        filters: {
            getMoneyNum
        },
        components: {
            ViewCard,
            DataChart,
            ScatterPlot
        }
    }
</script>

<style scoped lang="scss">
    .dashboard-container {
        .echart-table-box {
            .el-col {
                .content {
                    position: relative;
                    .pie-tips {
                        display: flex;
                        justify-content: flex-start;
                        align-items: center;
                        padding-left: 15px;
                        margin-top: 8px;
                        div {
                            width: 26%;
                            margin-right: 3%;
                            p {
                                font-size: 14px;
                                color: #212225;
                                line-height: 18px;
                                margin: 0;
                                &:first-child {
                                    font-size: 16px;
                                    font-weight: 500;
                                }
                            }
                            &:last-child {
                                width: auto;
                                p:first-child {
                                    color: #FF4532;
                                }
                            }
                        }
                    }
                }
                &:nth-child(3), &:nth-child(4) {
                    margin-bottom: 0;
                }
            }
        }
    }
</style>
